﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;

namespace Carwash
{
    public partial class CarwashGUI : Form
    {
        #region variables
            private int isSelected = 0;  
            private double deluxePrice = 4.00;
            private double ultraPrice = 10.00;
            private double superPrice = 8.00;
            private int pin;
            private int password;
            private int username;
            private bool isCarwashRequest = true;
            private static System.Timers.Timer lockOutTimer;
            CW_EventHandler event_handler = new CW_EventHandler();
            //CW_Security_Facade security_facade = new CW_Security_Facade();
            //CW_Management_Facade management_facade = new CW_Management_Facade();
        #endregion

        #region constants
            private const int ENTER_PIN = 100;
            private const string Deluxe_Wash = "Deluxe Wash $";
            private const string Super_Wash = "Super Wash $";
            private const string Ultra_Wash = "Ultra Wash $";
            private const int MANAGER = 1;
            private const int OPERATOR = 2;
            private const int DELUXE = 3;
            private const int SUPER = 4;
            private const int ULTRA = 5;
            private const int INVALID_PIN = 0;
        #endregion

        public CarwashGUI()
        {
            InitializeComponent();
        }

        private void CarwashGUI_Load(object sender, EventArgs e)
        {

        }


        #region mainsScreen
        //car wash buttons
        private void deluxeWashBtn_Click(object sender, EventArgs e)
        {
            deluxeWashBtn.Enabled = false;
            ultraWashBtn.Enabled = true;
            superWashBtn.Enabled = true;
            processBtn.Enabled = true;
            selectedServLbl.Text = Deluxe_Wash + deluxePrice;
            isSelected = 1;
        }

        private void ultraWashBtn_Click(object sender, EventArgs e)
        {
            deluxeWashBtn.Enabled = true;
            ultraWashBtn.Enabled = false;
            superWashBtn.Enabled = true;
            processBtn.Enabled = true;
            selectedServLbl.Text = Ultra_Wash + ultraPrice;
            isSelected = 1;
        }

        private void superWashBtn_Click(object sender, EventArgs e)
        {
            deluxeWashBtn.Enabled = true;
            ultraWashBtn.Enabled = true;
            superWashBtn.Enabled = false;
            processBtn.Enabled = true;
            selectedServLbl.Text = Super_Wash + superPrice;
            isSelected = 1;
        }

        private void enterPinBtn_Click(object sender, EventArgs e)
        {
            invalidPinLbl.Visible = false;
            topLbl.Text = "Please type your pin";
            enterPinPanel.Visible = true;
            cancelOrderBtn.Visible = true;
            processBtn.Visible = false;
            pin = 0;

            selectedServLbl.Text = "Service that you select will be shown here.";
        }

        #endregion

        // sidePanel (infromation panel and cummon btns)
        private void cancelOrderBtn_Click(object sender, EventArgs e)
        {  
            this.clear();
        }

        private void helpBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Please Call 1-800-CWHELP", "Carwash  Help", MessageBoxButtons.OK) == DialogResult.OK)
            {
                //......
            }
        }
        
        // enter pin keypad btns

            private void button1_Click(object sender, EventArgs e)
            {
                setTextBox(1);
            }

            private void button2_Click(object sender, EventArgs e)
            {
                setTextBox(2);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                setTextBox(3);
            }

            private void button4_Click(object sender, EventArgs e)
            {
                setTextBox(4);
            }

            private void button5_Click(object sender, EventArgs e)
            {
                setTextBox(5);
            }

            private void button6_Click(object sender, EventArgs e)
            {
                setTextBox(6);
            }

            private void button7_Click(object sender, EventArgs e)
            {
                setTextBox(7);
            }

            private void button8_Click(object sender, EventArgs e)
            {
                setTextBox(8);
            }

            private void button9_Click(object sender, EventArgs e)
            {
                setTextBox(9);
            }

            private void button10_Click(object sender, EventArgs e)
            {
                setTextBox(0);
            }

            private void setTextBox(int digit)
            {
                if (pinTextBox.Text.Length<9)
                    pinTextBox.Text = pinTextBox.Text + digit;

            }

            private void backSpaceBtn_Click(object sender, EventArgs e)
            {
                if (pinTextBox.Text.Length > 0)
                {
                    pinTextBox.Text = pinTextBox.Text.Substring(0, pinTextBox.Text.Length - 1);
                }
            }

            private void enterBtn_Click(object sender, EventArgs e)
            {
                if (pinTextBox.Text.Length > 0)
                {
                    password = Convert.ToInt32(pinTextBox.Text);
                    if (isCarwashRequest)
                    {
                        pinValidation();
                    }
                    else
                    {
                        if (enterPassword(username, password))
                        {
                            enableManagerScreen();
                            isCarwashRequest = true;
                            invalidPinLbl.Visible = false;
                            invalidPinLbl.Text = "Invalid pin, please try again!";
                            cancelOrderBtn.Visible = false;
                            processBtn.Visible = false;
                        }
                        else
                        {
                            invalidPinLbl.Text = "Invalid Password, please try again!";
                            invalidPinLbl.Visible = true;
                            if (event_handler.maxNumPasswordAttemptsExceeded())
                            {
                                lockPinScreen();
                            }
                        }
                    }
                                       
                }
                else
                {
                    invalidPinLbl.Visible = true;
                }
                pinTextBox.Text = "";
            }

            private void lockPinScreen()
            {
                if (MessageBox.Show("Too many invalid attempts", "Security Alert", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    clear();                    
                    enterPinBtn.Enabled = false;
                    CheckForIllegalCrossThreadCalls = false;
                    lockOutTimer = new System.Timers.Timer(10000);
                    lockOutTimer.Elapsed += new ElapsedEventHandler(PinUnlock);
                    lockOutTimer.Enabled = true;
                }
            }

            private void PinUnlock(object source, ElapsedEventArgs e)
            {
                isCarwashRequest = true;
                event_handler.resetPasswordAttempts();
                event_handler.resetPinAttempts();
                enterPinBtn.Enabled = true;
                lockOutTimer.Enabled = false;
            }
            

            private bool enterPassword(int username, int password)
            {
                return event_handler.validatePassword(username, password);
            }

            private void cancelPinBtn_Click(object sender, EventArgs e)
            {
                pinTextBox.Text = "";
                invalidPinLbl.Visible = false;
                selectedServLbl.Text = "Service that you select will be shown here.";
            }

            private void cancelPinBtn_Click()
            {
                
                enterPinPanel.Visible = false;
                pinTextBox.Text = "";
                cancelOrderBtn.Visible = false;
                processBtn.Visible = false;
            }

        #region Manager Screen

            private void turnOffSystem_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("System Turn Off Requested."," Are you sure?", MessageBoxButtons.YesNo)== DialogResult.Yes)
                {
                    this.Close();
                }
            }

            private void printLogs_Click(object sender, EventArgs e)
            {
                logsTextBox.Text = event_handler.displayLogs();
            }

            private void logOutButton_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Are you sure?", "Logout Requested", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    topLbl.Text = "Please make your selection";
                    managerPanel.Visible = false;
                    isCarwashRequest = true;
                    clear();
                }
            }

            private void clearLogsBtn_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Logs cannot be recovered after deletion. Are you sure you want to delete all log entries?", "Warning", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    event_handler.clearLogs();
                    logsTextBox.Text = "";
                }
            }
        #endregion

        #region helpers
            private void clear()
            {
                deluxeWashBtn.Enabled = true;
                ultraWashBtn.Enabled = true;
                superWashBtn.Enabled = true;
                isSelected = 0;
                invalidPinLbl.Visible = false;
                enterPinPanel.Visible = false;
                selectedServLbl.Text = "Service that you select will be shown here.";
                topLbl.Text = "Please make your selection";
                pinTextBox.Text = "";
                cancelOrderBtn.Visible = true;
                processBtn.Visible = true;
                processBtn.Enabled = false;
                isCarwashRequest = true;
            }

            private void pinValidation()
            {
                pin = Convert.ToInt32(pinTextBox.Text);
                int pin_type = event_handler.validatePin(pin);
                string message;
                isSelected = pin_type;
                switch (pin_type)
                {
                    case MANAGER:
                        {
                            message = "Manager";
                            username = pin;
                            isCarwashRequest = false;
                            topLbl.Text = "Please Enter Password";
                            invalidPinLbl.Visible = false;
                            pinTextBox.Text = "";
                        } break;
                    case OPERATOR:
                        {
                            message = "Operator";
                            username = pin;
                            isCarwashRequest = false;
                            invalidPinLbl.Visible = false;
                            topLbl.Text = "Please Enter Password";
                            pinTextBox.Text = "";
                        } break;
                    case DELUXE:
                        {
                            message = "Deluxe Carwash";                            
                            this.cancelPinBtn_Click();
                        } break;
                    case SUPER:
                        {
                            message = "Super Carwash";
                            this.cancelPinBtn_Click();
                        }
                        break;
                    case ULTRA:
                        {
                            message = "Ultra Carwash";
                            this.cancelPinBtn_Click();
                        } break;
                    default:
                        {
                            invalidPinLbl.Visible = true;
                            message = "Service that you select will be shown here.";
                            if (event_handler.maxNumPinAttemptsExceeded())
                            {
                                lockPinScreen();
                            }
                            break;
                        }
                }
                selectedServLbl.Text = message;

                disableSelectedCarWasBtn();
            }

            private void disableSelectedCarWasBtn()
            {
                deluxeWashBtn.Enabled = true;
                ultraWashBtn.Enabled = true;
                superWashBtn.Enabled = true;
                processBtn.Visible = true;
                cancelOrderBtn.Visible = true;

                switch (isSelected)
                {
                    case DELUXE:
                        {
                            deluxeWashBtn.Enabled = false;
                            processBtn.Enabled = true;

                        }break;
                    case SUPER:
                        {
                            superWashBtn.Enabled = false;
                            processBtn.Enabled = true;
                        } break;
                    case ULTRA:
                        {
                            ultraWashBtn.Enabled = false;
                            processBtn.Enabled = true;
                        } break;
                    case MANAGER:
                        {
                            processBtn.Visible = false;
                        } break;
                    case OPERATOR:
                        {
                            processBtn.Visible = false;
                        } break;
                    default:
                        {
                            processBtn.Visible = false;
                            processBtn.Enabled = false;
                            break;
                        }
                }
            }
            private void enableManagerScreen()
            {
                managerPanel.Visible = true;
                topLbl.Text = "Manager Screen";
            }
        #endregion

    }
}
